package com.ruixinzheng.mvidemo.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ruixinzheng.mvidemo.R


class MainAdapter(
    private var list: ArrayList<String>
) : RecyclerView.Adapter<MainAdapter.DataViewHolder>() {

    class DataViewHolder: RecyclerView.ViewHolder {
        private lateinit var tv:TextView

        constructor(itemView:View) : super(itemView) {
            tv = itemView.findViewById(R.id.tv)
        }

        fun bind(str: String) {
            tv.text = str
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        DataViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.adapter_main, parent,
                false
            )
        )

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: DataViewHolder, position: Int) =
        holder.bind(list[position])

    fun setData(list: ArrayList<String>) {
        this.list = list
        notifyDataSetChanged()
    }

}
